package com.system.dao.BookDao;

import com.system.dao.BaseDao;
import com.system.pojo.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BookDaoImpl implements BookDao{

    @Override
    public List<Object> SelectBookList(Connection connection, String BookType, String BookName, String BookAuthor) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        List<Object> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        List<Object> books = new ArrayList<>();

        /*
        * 三个变量，判断是否为空后连接sql语句进行查询
        * 然后返回查询结果
        * */

        if(connection != null){
            System.out.println("数据库不为空");
            sql.append("select * from book ");//全查询
            //判断第一个是否有参数是否有值
            int index = 0;
            if(!BookType.equals(""))//equals() 如果相同为True
            {
                index = 1;
                sql.append("where BookType = ? ");
                //类型不需要模糊搜索
                list.add(BookType);
            }
            if(!BookName.equals("")){
                if(index == 1){
                    sql.append("and BookName like ? ");
                }else{
                    sql.append("where BookName like ? ");
                }
                index = 1;
                list.add("%"+BookName+"%");

            }
            if(!BookAuthor.equals("")){
                if(index == 1){
                    sql.append("and BookAuthor like ?");
                }else {
                    sql.append("where BookAuthor like ?");
                }
                list.add("%"+BookAuthor+"%");
            }
            //--------------分割线-----------------//
            //检查一下sql语句是否正确
            System.out.println("sql -->"+sql.toString());
            Object[] o = list.toArray();
            System.out.println("o -->"+ Arrays.toString(o));
            resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql.toString(),o);
            while(resultSet.next()){
                Book book = new Book();
                book.setBookId(resultSet.getInt("BookId"));
                book.setBookType(resultSet.getString("BookType"));
                book.setBookNub(resultSet.getInt("BookNub"));
                book.setBookName(resultSet.getString("BookName"));
                book.setBookAuthor(resultSet.getString("BookAuthor"));
                book.setBookPrice(resultSet.getInt("BookPrice"));
                book.setBookImgPath(resultSet.getString("BookImgPath"));
                book.setBookIssueDate(resultSet.getString("BookIssueDate"));
                book.setBookIntroduces(resultSet.getString("BookIntroduces"));
                books.add(book);
            }
            System.out.println("Dao中books："+books.toString());
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return books;
    }

    @Override
    public List<Object> SelectTypeList(Connection connection) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Object[] o = {};
        String sql = "";
        List<Object> type = new ArrayList<>();
        if(connection != null){
            sql = "select BookType from book";
            resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql,o);
            while(resultSet.next()){
                type.add(resultSet.getString("BookType"));
            }
        }
        System.out.println("type:"+type);
        return type;
    }


}
